package com.thinkjoy.util;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;

/**
 * Created by shaojun on 16-9-8.
 */
public class PathHandle {

    /**
     * 获取相对路径
     * @return
     */
    public static String getRelativePath() {
        URL url = PathHandle.class.getProtectionDomain().getCodeSource().getLocation();
        String filePath = null;
        try {
            filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (filePath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"
            // 截取路径中的jar包名
            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        }

        File file = new File(filePath);
        filePath = file.getAbsolutePath();//得到windows下的正确路径
        return filePath;
    }

}
